package com.example.tool.adapter;

import android.support.annotation.NonNull;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.IExpandable;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.example.tool.R;
import com.example.tool.bean.RexueDetail;
import com.example.tool.bean.RexueJobBean;
import com.example.tool.bean.RexueWeapon;
import com.example.tool.util.ConstantVale;
import com.example.tool.util.DebugLog;

import java.util.List;

import static com.example.tool.util.DebugLog.ADAPTER;

/**
 * Created Date 2019/9/23
 *
 * @author liangkun
 * description：伸缩扩展
 */
public class ExpandAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private boolean isOnlyExpandOne = false; //是否只显示一个

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ExpandAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(ConstantVale.REXUE_JOB, R.layout.item_expandable_lv0);
        addItemType(ConstantVale.REXUE_WAPON, R.layout.item_expandable_lv1);
        addItemType(ConstantVale.REXUE_PERSON, R.layout.item_expandable_lv2);
    }

    public void setOnlyExpandOne(boolean onlyExpandOne) {
        isOnlyExpandOne = onlyExpandOne;
    }

    @Override
    protected void convert(@NonNull final BaseViewHolder holder, final MultiItemEntity item) {
        switch (holder.getItemViewType()) {
            case ConstantVale.REXUE_JOB:
                final RexueJobBean lv0 = (RexueJobBean) item;
                holder.setText(R.id.title, lv0.getJob())
                        .setImageResource(R.id.iv, lv0.isExpanded() ? R.mipmap.arrow_b : R.mipmap.arrow_r);
                if (lv0.getJob().equals("热血江湖-刀")) {
                    holder.setImageResource(R.id.iv_head, R.mipmap.dao);
                } else if (lv0.getJob().equals("热血江湖-枪")) {
                    holder.setImageResource(R.id.iv_head, R.mipmap.qiang);
                } else if (lv0.getJob().equals("热血江湖-剑")) {
                    holder.setImageResource(R.id.iv_head, R.mipmap.jian);
                } else if (lv0.getJob().equals("热血江湖-医")) {
                    holder.setImageResource(R.id.iv_head, R.mipmap.yisheng);
                } else {
                    holder.setImageResource(R.id.iv_head, R.mipmap.gong);
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int pos = holder.getAdapterPosition();
                        DebugLog.d(ADAPTER + " lv0.isExpanded()--- " + lv0.isExpanded());
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else if (isOnlyExpandOne) {
                            IExpandable willExpandItem = (IExpandable) getData().get(pos);
                            for (int i = getHeaderLayoutCount(); i < getData().size(); i++) {
                                IExpandable expandable = (IExpandable) getData().get(i);
                                if (expandable.isExpanded()) {
                                    DebugLog.d(ADAPTER + " 关闭其他的**** " + lv0.isExpanded());
                                    collapse(i);
                                }
                            }
                            expand(getData().indexOf(willExpandItem) + getHeaderLayoutCount());
                        } else {
                            DebugLog.d(ADAPTER + " lv0.isExpanded()**** " + lv0.isExpanded());
                            expand(pos);
                        }
//                        int pos = holder.getAdapterPosition();
//                        DebugLog.d(ADAPTER + " lv0.isExpanded()--- " + lv0.isExpanded());
//                        if (lv0.isExpanded()) {
//                            collapse(pos);
//                        } else {
//                            expand(pos);
//                        }
//                        DebugLog.d(ADAPTER + " lv0.isExpanded()**** " + lv0.isExpanded());
//                        if (isOnlyExpandOne) {
//                            IExpandable willExpandItem = (IExpandable) getData().get(pos);
//                            for (int i = getHeaderLayoutCount(); i < getData().size(); i++) {
//                                IExpandable expandable = (IExpandable) getData().get(i);
//                                if (expandable.isExpanded()) {
//                                    DebugLog.d(ADAPTER + " 关闭其他的**** " + lv0.isExpanded());
//                                    collapse(i);
//                                }
//                            }
////                            expand(getData().indexOf(willExpandItem) + getHeaderLayoutCount());
//                        }
//
//
                    }
                });
                break;
            case ConstantVale.REXUE_WAPON:
                final RexueWeapon lv1 = (RexueWeapon) item;
                holder.setText(R.id.title, lv1.getZhuanZhi())
                        .setImageResource(R.id.iv, lv1.isExpanded() ? R.mipmap.arrow_b : R.mipmap.arrow_r);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();

                        if (lv1.isExpanded()) {
                            collapse(pos, false);
                        } else {
                            expand(pos, false);
                        }
                    }
                });

//                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                    @Override
//                    public boolean onLongClick(View v) {
//                        int pos = holder.getAdapterPosition();
//                        // 先获取到当前 item 的父 positon，再移除自己
//                        int positionAtAll = getParentPositionInAll(pos);
//                        remove(pos);
//                        if (positionAtAll != -1) {
//                            IExpandable multiItemEntity = (IExpandable) getData().get(positionAtAll);
//                            if (!hasSubItems(multiItemEntity)) {
//                                remove(positionAtAll);
//                            }
//                        }
//                        return true;
//                    }
//                });
                break;
            case ConstantVale.REXUE_PERSON:
                final RexueDetail person = (RexueDetail) item;
                holder.setText(R.id.tv, person.getWeaponDetail() + " parent pos: " + getParentPosition(person));
//                holder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        int pos = holder.getAdapterPosition();
//                        // 先获取到当前 item 的父 positon，再移除自己
//                        int positionAtAll = getParentPositionInAll(pos);
//                        remove(pos);
//                        if (positionAtAll != -1) {
//                            IExpandable multiItemEntity = (IExpandable) getData().get(positionAtAll);
//                            if (!hasSubItems(multiItemEntity)) {
//                                remove(positionAtAll);
//                            }
//                        }
//                    }
//                });
                break;
            default:
                break;
        }
    }
}
